// miniprogram/pages/blog/blog.js
import util from '../../utils/util.js'
let isend = true // 判断是否是最后一页
const COUNT = 10
let keyword = ''

Page({

  /**
   * 页面的初始数据
   */
  data: {
    modalShow: false,
    commentShow: false,
    blogList: [],
    isLoading: false,
    userInfo: '',
    selectblogId: '',
    selectblog: '',
    publish: false,


    isRefreshing: false,//是否下拉刷新状态
    isFinish: false,//是否加载完全部数据
    
  },
  onPublish() {
    util.getUserInfo().then(res => {
      this.loginSuccess({
        detail: res
      })
    }).catch(err => {
      this.setData({
        modalShow: true
      })
    })
  },
  loginSuccess(event) {
    console.log(event)
    const detail = event.detail
    wx.navigateTo({
      url: `../blog-edit/blog-edit?nickName=${detail.nickName}&avatarUrl=${detail.avatarUrl}`,
    })
  },
  loginFail() {
    wx.showToast({
      title: '请完成授权后才可以发布',
      icon: 'none'
    })
  },

  onSearch(event){
    console.log(event)
    keyword = event.detail.keyword
    this._getblogList()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getblogList()
    // let params = wx.getStorageSync('params')
    // this.setData({
    //   publish: params.publish
    // })
    this.getparams()

  },

  _getblogList(start = 0) {
    wx.cloud.callFunction({
      name: 'blog',
      data: {
        keyword,
        start,
        count: COUNT,
        $url: 'list'
      }
    }).then((res) => {
      console.log(res)
      if (start === 0) {
        this.data.blogList = []
      }
      let data = res.result.data
      let total = res.result.total
      if (data.length > 0) {
        this.setData({
          blogList: this.data.blogList.concat(res.result.data),
          alreadyLoadData: true,
          isLoading: false,
          isFinish: res.result.data.length >= COUNT ? false : true
        })
      }
      this.setData({
        isRefreshing: false,
        isEmpty: total > 0 ? false : true
      })
      
      
    })
  },
  
  getparams() {
    wx.cloud.callFunction({
      name: 'getParams'
    }).then(res => {
      console.log('params', res)
      let result = res.result
      if (result.code === 200) {
        let {
          publish
        } = result
        let params = {
          publish
        }
        this.setData({
          publish: params.publish
        })
      }
    })
  },
  

  goComment(event) {
    wx.navigateTo({
      url: '../../pages/blog-comment/blog-comment?blogId=' + event.target.dataset.blogid,
    })
  },

  changeModalShow(event) {
    let detail = event.detail
    this.setData({
      commentShow: true,
      selectblogId: detail.blogId,
      selectblog: detail.blog,
      userInfo: detail.userInfo
    })
  },
  changeLoginShow(event) {
    let detail = event.detail
    this.setData({
      loginShow: true,
      selectblogId: detail.blogId,
      selectblog: detail.blog,
    })
  },
  

  onRefresh() {
    this._getblogList(0)
  },

  onloadMore() {
    console.log('触底了', this.data.isFinish)
    if (!this.data.isFinish) {
      this._getblogList(this.data.blogList.length)
      this.setData({
        isLoading: true
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this._getblogList(0)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (event) {
    console.log(event)
    var imageUrl = '', title = '轻松发现好内容', path = 'pages/blog/blog'
    if (event.from == 'button') {
      let blogObj = event.target.dataset.blog
      title = blogObj.content
      path = `/pages/blog-comment/blog-comment?blogId=${blogObj._id}`
      if (blogObj.img && blogObj.img.length > 0) {
        imageUrl = blogObj.img[0]
      }
    }
    return {
      title,
      imageUrl,
      path
    }
  }
})